package com.parking.common.core.util;

import com.parking.common.core.exception.TokenException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    /*
    * user 用户 admin 管理员 renters 出租客 centified 物业
    * */
    private static final  String userSerret="ksaasfdasdfadsfwfrasdfsadff354943casijfasadfasfdlkfjlasjflksajfd";
    private static final  String adminSerret="ksaasfdasdfaasjfvcdmaks15345dffcasijfasadfasfdlkfjlasjflksajfd";

    private static final  String rentersSerret="safdsafdgfea2414325tjkljlfdqereqr09243809542rmsalkdnflkanmgwaegf";

    private static final  String certifiedSerret="jkljfldsafewr9093245kjl432j5209lkj3lkqrlk23q5rlknsadlkgfn43ot";

    /**
     * 生成Token
     */
    public  static String createToken(Map<String,Object> body,Integer time,String secret){
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
        JwtBuilder jb=Jwts.builder();
        if (body==null||body.size()<=0){
            throw new TokenException(7001,"没有数据");
        }
        jb.setClaims(body);
        if(time==null){
            jb.setExpiration(new Date(System.currentTimeMillis()+ 60 * 30 * 10000));
        }else {
            jb.setExpiration(new Date(System.currentTimeMillis()+time * 10000));
        }
        return jb.signWith(key).compact();
    }
    /**
     * 生成Token
     */
    public  static String createToken(Map<String,Object> body,String secret){
        return createToken(body,null,secret);
    }
    /**
     * 解析Token
     */
    public static Claims parseToken(String token,String secret)throws Exception{
//        System.out.println("//////////////////////////////////////////////");
        if(token==null){
            throw  new TokenException(7002,"token为空");
        }
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
        Claims claims = Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
        return claims;
    }
    /**
     * 生成用户端token
     *
     */
    public static String createUserToken(HashMap<String, Object> body){
        return createToken(body,userSerret);
    }
    /**
     * 生成用户端token
     *
     */
    public static String createUserToken(Integer time,Object... body){
        if(body==null||body.length<=0) throw  new TokenException(7002,"token为空");
        HashMap<String, Object> map = new HashMap<>();
        System.out.println(map);
        for (int i = 0; i < body.length; i+=2) {
            Object value=null;
            if (i+1<body.length) {
                value=body[i+1];
            }
            map.put(body[i].toString(),value);
        }
        return createToken(map,userSerret);
    }
    /**
     * 生成管理员token
     *
     */
    public static String createAdminToken(HashMap<String, Object> body){
        return createToken(body,adminSerret);
    }
    /**
     * 生成管理员token
     *
     */
    public static String createAdminToken(Integer time,Object... body){
        if(body==null||body.length<=0) {
            System.out.println(body+"----------------------------");
            throw  new TokenException(7002,"token为空");
        }
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < body.length; i+=2) {
            Object value=null;
            if (i+1<body.length) {
                value=body[i+1];
            }
            map.put(body[i].toString(),value);
        }
        return createToken(map, time, adminSerret);
    }
    /**
     * 生成出租客token
     *
     */
    public static String createRentersToken(HashMap<String, Object> body){
        return createToken(body,rentersSerret);
    }
    /**
     * 生成出租客token
     *
     */
    public static String createRentersToken(Integer time,Object... body){
        if(body==null||body.length<=0) throw  new TokenException(7002,"token为空");
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < body.length; i+=2) {
            Object value=null;
            if (i+1<body.length) {
                value=body[i+1];

            }
            map.put(body[i].toString(),value);
        }
        return createToken(map,rentersSerret);
    }
    /**
     * 生成物业token
     *
     */
    public static String createCertifiedToken(HashMap<String, Object> body){
        return createToken(body,certifiedSerret);
    }
    /**
     * 生成物业token
     *
     */
    public static String createCentifiedToken(Integer time,Object... body){
        if(body==null||body.length<=0) throw  new TokenException(7002,"token为空");
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < body.length; i+=2) {
            Object value=null;
            if (i+1<body.length) {
                value=body[i+1];

            }
            map.put(body[i].toString(),value);
        }
        return createToken(map,certifiedSerret);
    }
    /**
     * 解析用户token
     *
     */
    public static Claims parseUserToken(String token) throws Exception{
        return parseToken(token,userSerret);
    }
    /**
     * 解析管理员token
     *
     */
    public static Claims parseAdminToken(String token)throws Exception{
        return parseToken(token,adminSerret);
    }
    /**
     * 解析出租客token
     *
     */
    public static Claims parseRendersToken(String token)throws Exception{
        return parseToken(token,rentersSerret);
    }
    /**
     * 解析物业token
     *
     */
    public static Claims parseCertifiedToken(String token)throws Exception{
        return parseToken(token,certifiedSerret);
    }

    public static Integer getUserId(String token) throws Exception {
        String userIdStr = parseUserToken(token).get("userId").toString();
        return Integer.parseInt(userIdStr);
    }

    public static Integer getCertifiedAccountId(String token) throws Exception {
        return (Integer) parseCertifiedToken(token).get("certifiedAccountId");
    }

    public static Integer getPlatformId(String token) throws Exception {
        String platformId = parseAdminToken(token).get("platformId").toString();
        return Integer.parseInt(platformId);
    }

    public static Integer getRentersId(String token) throws Exception {
        return (Integer) parseRendersToken(token).get("rentersId");
    }


    public static void main(String[] args) throws Exception {
        String s1 = "eyJhbGciOiJIUzM4NCJ9.eyJwbGF0Zm9ybUlkIjoxLCJleHAiOjE3MDIyODYxNjB9.0C6SMphOFiplBTKxq0EnwKvZi47amsEjQXXGrQHOq-QNYWJTBvzW-yQ1iDXUcYm1";
        String s = "eyJhbGciOiJIUzM4NCJ9.eyJwbGF0Zm9ybUlkIjoxLCJleHAiOjE3MDIyODk5Mzh9.6-YNPrqrpSi27XrF_CWmPH2gjJagauj1BgdYCPRPhHuMctPnwuBR8C5gtHOBghcX";
        parseAdminToken("eyJhbGciOiJIUzM4NCJ9.eyJwbGF0Zm9ybUlkIjoxLCJleHAiOjE3MDIzMjg1MDN9.yDo38mrukBFZMEu4Z6Wu7qnEuKi5Mdtc2KIb8Hecz0WCZonxkHwyzq891E7nShi1");
    }





}
